//1.引入express
const { request, response } = require('express')
const express = require('express')

//2.创建应用对象
const app = express()

//3.创建路由规则
//request是对请求报文的封装
//response是对响应报文的封装
app.get('/server', (request, response) => {
    //设置相应
    //响应头    设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //响应体
    response.send("hello ajax");
})

app.post('/server', (request, response) => {
    //设置相应
    //响应头    设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //响应体
    response.send("hello ajax");
})
app.all('/json-server', (request, response) => {
    //设置相应
    //响应头    设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //相应数据
    const data = {
            name: 'wzc'
        }
        //对对象进行字符串转化
    let str = JSON.stringify(data);
    //响应体
    response.send(str);
})
app.get('/srca', (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    setTimeout(() => {
        response.send('yanshi')
    }, 3000);
})
app.all('/jquery-server', (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    const data = {
        name: "wzc"
    }
    response.send(JSON.stringify(data));

})
app.all('/axios-server', (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-headers", "*");
    const data = {
        name: "wzc"
    }
    response.send(JSON.stringify(data));

})
app.all('/fetch-server', (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-headers", "*");
    const data = {
        name: "wzc"
    }
    response.send(JSON.stringify(data));

})
app.get('/jsonp-server', (request, response) => {
    // response.send("console.log('hello')")
    const data = {
        name: 'wzc'
    }
    let str = JSON.stringify(data);
    response.end('hendle(' + str + ')');
})
app.get('/username-server', (request, response) => {
    // response.send("console.log('hello')")
    const data = {
        exist: 1,
        msg: '用户名已经存在'
    }
    let str = JSON.stringify(data);
    response.end('hendle(' + str + ')');
})
app.get('/a', (request, response) => {
        // response.send("console.log('hello')")
        const data = {
            name: 'wzc'
        }
        let cd = request.query.callback;
        let str = JSON.stringify(data);
        response.end(cd + '(' + str + ')');
    })
    //4.监听端口服务
app.listen(8080, () => {
    console.log("服务已经启动，8080 端口监听中")
})